﻿using System;
using System.Collections.Generic;
using System.Net;

namespace Tars.Net.Configurations
{
    public class RpcConfiguration
    {
        public int Port { get; set; } = 8989;

        public int QuietPeriodSeconds { get; set; } = 1;

        public TimeSpan QuietPeriodTimeSpan => TimeSpan.FromSeconds(QuietPeriodSeconds);

        public int ShutdownTimeoutSeconds { get; set; } = 3;

        public TimeSpan ShutdownTimeoutTimeSpan => TimeSpan.FromSeconds(ShutdownTimeoutSeconds);

        public int SoBacklog { get; set; } = 8192;

        public int MaxFrameLength { get; set; } = 5 * 1024 * 1024;

        public int LengthFieldLength { get; set; } = 4;

        public IDictionary<string, ClientConfiguration> ClientConfig { get; set; } = new Dictionary<string, ClientConfiguration>(StringComparer.OrdinalIgnoreCase);

        public int EventLoopCount { get; set; } = Environment.ProcessorCount;

        public int UdpLocalPort { get; set; } = 8787;

        public string Host { get; set; }

        private EndPoint endPoint;

        public EndPoint EndPoint
        {
            get
            {
                if (endPoint == null)
                {
                    if (IPAddress.TryParse(Host, out IPAddress ip))
                    {
                        endPoint = new IPEndPoint(ip, Port);
                    }
                    else
                    {
                        endPoint = new DnsEndPoint(Host, Port);
                    }
                }
                return endPoint;
            }
        }
    }
}